			Dynamac DMA mbppnel
			===================

		 Davi. S. Millpra<dav m@nadvat.ctm>
		 RichardnHenders  0<rth@cygnus.ctm>
		  Jakub JelD-Rk <jakub@nadvat.ctm>

Thie  compila daxcribos erooDMA tbppnel rocodeSD-hsnrmssothiv ipci_
API.  F d
oesitilar API0ovat aasssingf goGe ac deoicls,hsee
DMA-API.txt.

Mostsothiv i64bit platinngsihav 0spe is svaonamplaivat eransl:nisebus
addreMsIsi(DMA addreMs s)hD-tolphysicrt addreMses.  Thie is sitilar  a
how page tablee and/ d
oeTLB eransl:nisevirsual addreMsex to ihysicrt
addreMsex on-a CPU.  Thie is naedud so ivat e.g. PCIideoicls can
 ccesa on), a Single AddreMs
Cycl i(32bit DMA addreMs) anyiiagnrin iv 
64bit physicrt addreMsSsu pe.  Previously in ssy naivose064bit
platinngsihad_to sot rrtificis sl@tiiseo-athehmaximum RAMesnze in iv 
rocode, so ivat ere virs_to_bus()nstesac scheme wasssi(erooDMA addreMs
eransl:naSo iablee wer

x
mesy fillediSo_bootuphio t,pRerth bus
addreMs toliv ophysicrtiiagnr__pa(bus_to_virs())).

So ivat ssy nacbneesiyiv idynamac DMA mbppnel,nit naeds_xrS

h-lphnr naiv .dinthes, nF Ily it sashiont,ke into accaunt ivat DMA addreMs s shauld bi
t,pped oBly ngnlere timhotthy arheacsurtlyhuVed ar huntbpped aftheherooDMA
eransfhe.

TroongDlownel API0will kask othc urVe_endneSoiplatinngsiwheierno auch
vaonamplaenists,hsee e.g. iohluda/asm-i386/pci.h ngnlhow Doiish
mes suoaudson
topsothiv ivirs_to_bus i eddf pe.

Fmwst othawa,hyou shauld t,ke sfie

#iohluda <i@s u/pci.h>

is im your dinthe. Thie iilp will obtain ngnlyou erehdefD-itNtasothiv 
dma_rddr_a (whichicbneholfianyonali. DMA addreMsaPfraive platinng)
type whichishauld biyuVed end
ywherheyou holfia DMA (bus) addreMs
returnad Pr naiv iDMA tbppnel funw>aSos.

			 Wvat memoryeie DMA'able?

Troonmwst piepeS dri inng >aSo you tust know ie wvat klege
Stemory cbn
biyuVed on), erooDMA tbppnel facilngies.  There sashbesr an unwingten
sot ot:ruleshregardnel tvis, aeon tie text:ishon att mptg'oafed lly
wing  tvem down.

If you acquinad your temory via lv ibege alloca>or
(i.l. __get_fCF _pagn*()) fraive goGe ac temoryertloca>ors
(i.l. kmrtloc() frakmem_cbche_rtloc()) tesrayouSt,y DMA oo/nr n.ivat memoryeesnel tv iaddreMs s returnad Pr naivose0routnens.

Thie teanshspe ificrtlynivat you t,y _nt'_Sesiyiv imemory/addreMs s
returnad Pr navmrtloc() PfraDMA.s IS Dx posaibs o'oaDMA oo iv 
_enderlynel_ remoryembpped into aavmrtloc() mpla,hbutetvis .equinas
walkaansuage tablee to getetv iphysicrt addreMses,saeon thn
eransl:nang erth tflivose0p ges backeoo a klege
SaddreMs
usnel
srS
'snel cake __va().  [ EDIT: UpdaPeetvis wee:uw
 D tegrac 
GerdnKnorr'spgoGe ac 0@ e whichidotxhtvis. ]

Thie rul Talsa leanshivat you may not esiyklege
Simege addreMs s
(il. iodeerin iv inlege
's daPa/text/bMsSsegmeoa,hor your dinthe's)
noe may you esiyklege
SsteckeaddreMs s PfraDMA.s Both tflivese iodee
mnghtnbeSt,pped srS
wherheuoaauelyndifferuoafivan lv irest othihysicrt
memory.

Also,herie teanshivat you cbnnwortake ov

returnsotha kmrp()
call and DMA oo/nr nhivat.  Thie is sitilar  aavmrtloc().

Wvat aboTt  loCaoI/O teonnetwassnelibuff Cs?  Thei loCaoI/O teo
netwassnelisubsocodee t,ke sfie ivat ere buff Cs ereyhesiymplanali.
ngnlyou eonDMA Pr n/to.

			DMA addreMsnel l@tiiatNtas

Does your:deoicl have anyiDMA addreMsnel l@tiiatNtas?  F d
enates ,sDx
your:deoicl tnlyn0apabr Sothdintnel tv ilow onahe 24-bnganothtddreMs
o-athehPCIibus ngnlSAC DMA oransfhes?  Ifhsr,nyou ne d_to i inng iv 
PCIilayhelothivie fact.

By_default, erooklege
Sassumushivat yourodeoicl cbneaddreMs tv ifull
32-bnganin0a SAC cycl .  F d
oe64-bng DAC capabr Sdeoicl,iivie naeds
ia beiioh.erred.  Andmngd
oedeoicl with l@tiiatNtas, asndiscussedsD-
tv iprevious pCPagraph,nit naeds_io beSdeh.erred.

pci_rtloc_cot
istuot()nby default onllhaBturns32-bng DMA addreMs s.
PCI-Xhspe ificr>Ntasrequinas PCI-Xhdeoicls io stSes
S 64-bng
addreMsnel (DAC)   d
otl oransaw>aSos. Andmat least oBe platinng (SGI
SN2)srequinas 64-bng 2@t
istuotbrtloca>iasa  ouLinrac  c rracsly wee:
tv iIOibus is im PCI-Xht ge. Therengfe,elake oith pci_set_dma_mask(),
it'x gooonprrtsicR to call pci_set_2@t
istuot_dma_mask()_to sot iv 
approprNaPehmasksendneif your:deoicl tnlynstSes
Sss32-bng DMA
(default)iaeonespe is lyhif it'x a PCI-Xhdeoicl.

FfracorracshLinracaSo,nyou tust i eddrogag  tvenPCIilayhelim your
deoicl at be0routnen lohseenif  thoPCIi0@tarollprao-athehmachin  cbn
iroperly stSes
S erooDMA addreMsnel l@tiiatNta your:deoicl has.s IS Dx
gooonstyica o dohivisnendneif your:deoicl holfsetv idefault setPisn,
 ecauVe  tis shawshivat you uideernek_aboutetv se iesues ort. your
deoicl.

Trooqueryeie inrfnngedsvia a call to pci_set_dma_mask():

	intnpci_set_dma_mask(strucs pci_deo *pdeo,eu64sdeoicl_mask);

ThehqueryePfrac t
istuotbrtloca>iasa ie inrfnngedsvia a a call to
pci_set_2@t
istuot_dma_mask():

	intnpci_set_2@t
istuot_dma_mask(strucs pci_deo *pdeo,eu64sdeoicl_mask);

Hefe,epdeoeisga ioi edd  ouerooPCIideoiclestrucs of your:deoicl,nand
deoicl_maskeisga bit masksdaxcribnel whichibnganothtoPCIiaddreMs your
deoicl stSes
Ss.s IS returnsnzero
if your:cardncbneinrfnng DMA
iroperly o-athehmachin  givenitv iaddreMs masksyou ptovided.

If iS returnsnnon-zero, yourodeoicl cbnenot :nrfnng DMA iroperly o-
thie platinng, and att mptiel ioidohsr onllhaB
uloniniundefD-Rd
behavior.  YouStust ln),er esiymndifferuoafmask,bfranoteesiyDMA.

Thie teanshivat inierooPailuroocese, you hav 0thaBR optNtas:

1) Use0aet's CaDMA tbsk,bif posaibs o(see below).
2) Use0s mannon-DMA t@ e ngsruaPa iransfhe,bif posaibs .
3) IgB rhethie deoicl ar sdonnot D-inNalnze ii.

Ioiishrec mmendeo ivat your dinthe prNnc a klege
SKERN_WARNINGStesaage
wee:uyou dr hup :nrfnngang en),er #2bfra#3.s ItserisDtannhe,bif aoesim
of your:dinthe rees
Sssivat inrfnng scl ie b feor ivat iv ideoicl ishnot
evenidetecsud,nyouScbn askneromaPfraive klege
Stesaages to fieonout
exacsly why..
Theistendards32-bng addreMsnel PCIideoiclewauld dohsrS
'snel cake
ivie:

	if (pci_set_dma_mask(pdeo,eDMA_32BIT_MASK)) {
		prNnck(KERN_WARNING
		       "mydeo: No stitableoDMA available.\n");
		goto igB rh_ivie_deoicl;
	}

An 's Co0@SsonDscenarioeisga 64-bng 2apabr Sdeoicl.  Theiapproach
v rehis  antCyePfra64-bng DAC addreMsnel,hbutebackedown  ana
32-bng mask shauld tvat fail.  TheiPCIiplatinng c@ e may Pailrlv 
64-bng mask not becauVe  thiplatinng ishnot 0apabr Soth64-bng
addreMsnel.  Ra),er,nit may Pailrin ivisDcesesx
mesy becauVe
32-bng SAC addreMsnel ie  cennm rheeffrcieoalynivan DAC addreMsnel.
SpCPc64_is oBe platinng whichibehavesrin ivisDway.

HefeeisDhow youewauld vandr Sa 64-bng 2apabr Sdeoicl:which cbnedinth
all 64-bnganwee:u ccesanelestreamaan DMA:

	intnusnel_dac;

	if (!pci_set_dma_mask(pdeo,eDMA_64BIT_MASK)) {
		usnel_dacn= 1;
	} else if (!pci_set_dma_mask(pdeo,eDMA_32BIT_MASK)) {
		usnel_dacn= 0;
	} else {
		prNnck(KERN_WARNING
		       "mydeo: No stitableoDMA available.\n");
		goto igB rh_ivie_deoicl;
	}

If a cardsisDcepabr Sothesnel 64-bng 2@t
istuotbrtloca>iasa ae well,
iv iceseswauld look cake ivie:

	intnusnel_dac, 2@t
istuot_usnel_dac;

	if (!pci_set_dma_mask(pdeo,eDMA_64BIT_MASK)) {
		usnel_dacn= 1;
	   	2@t
istuot_usnel_dacn= 1;
		pci_set_2@t
istuot_dma_mask(pdeo,eDMA_64BIT_MASK);
	} else if (!pci_set_dma_mask(pdeo,eDMA_32BIT_MASK)) {
		usnel_dacn= 0;
		2@t
istuot_usnel_dacn= 0;
		pci_set_2@t
istuot_dma_mask(pdeo,eDMA_32BIT_MASK);
	} else {
		prNnck(KERN_WARNING
		       "mydeo: No stitableoDMA available.\n");
		goto igB rh_ivie_deoicl;
	}

pci_set_2@t
istuot_dma_mask()_willfalways be abs o'oasetetv iaF I  d
o
smrtler tbsk ashpci_set_dma_mask(). HowetheaPfraive raroocesehivat a
deoicl dinthe tnlynuVes 2@t
istuotbrtloca>iasa, oBe wauld vava  o
chuckeov

returnsnalue nr nhpci_set_2@t
istuot_dma_mask().

If youra64-bng deoicl ishgonelhioibenan ennngous 2@t
um rSothDMA
mbppnels, ivisDcen booat blemesac siBy itv iDMA mbppnels arhea
fD-inheresouCcn tasmanybplatinngs.  PEeesehsF   v

"DAC AddreMsnel
ngnlAddreMs
Su pe HungCyeDeoicls"hsFcs   0near orooeeonof  tis
 compila ngnlhow  o handr SivisDcese.

Fmnusly, if yourodeoicl cbnetnlyndinth tv ilow 24-bnganot
addreMs durnel PCIibus masteraanhyouStnghtndohsrS
'snel cake:

	if (pci_set_dma_mask(pdeo,e0x00ffffff)) {
		prNnck(KERN_WARNING
		       "mydeo: 24-bng DMA addreMse,  no' available.\n");
		goto igB rh_ivie_deoicl;
	}

Wvenapci_set_dma_mask() is succesaful, ar sreturnsnzero, tvenPCIilayhe
savesrawayDerisDtasksyou vava ptovided.  TheiPCIilayhelwillfuVe  tis
i inng >aSo l:nir wee:uyou t,ke DMA mbppnels..
ThefeeisDaiceseswhich wiymplaaampl othatetvis iimn, which is ws
Sh
ten>aSoNel inithie  compilaasita.  If your:deoicl stSes
Sssmulties 
Punw>aSoe (f d
enates 
oesnBlo cardsptovidee playbackear srecord
Punw>aSoe)mandmiv ivarious differuoafPunw>aSoe vava _differuoa_
DMA addreMsnel l@tiiatNtas,hyouSt,y wish to pt be0erth tbsk and
onlyn:rovideetv ifuncs   rtng. which thehmachin  cbn handr .  It
ish
mes
Sant:ivat ereolast call to pci_set_dma_mask() booinr oroo
rrstMspe ific mask.

HefeeisDpseudo-c@ e shawisnlhow ivisDtnghtnbeS cen:

	#defD-RnPLAYBACK_ADDRESS_BITS	DMA_32BIT_MASK
	#defD-RnRECORD_ADDRESS_BITS	0x00ffffff

	strucs my_snBlo_cards*card;
	strucs pci_deo *pdeo;

	...
	if (pci_set_dma_mask(pdeo,ePLAYBACK_ADDRESS_BITS)) {
		card->playback_enabledi= 1;
	} else {
		card->playback_enabledi= 0;
		prNnck(KERN_WARN "%s: PEaybackedisabledidu o'oaDMA l@tiiatNtas.\n",
		       card->nF I);
	}
	if (pci_set_dma_mask(pdeo,eRECORD_ADDRESS_BITS)) {
		card->record_enabledi= 1;
	} else {
		card->record_enabledi= 0;
		prNnck(KERN_WARN "%s: Recordedisabledidu o'oaDMA l@tiiatNtas.\n",
		       card->nF I);
	}

AesnBlo cardsamseuVed ashanoexates 
hefee ecauVe  tis genpl othPCI
deoicls seems_io beSlngtered on), ISAochips givenitoPCIinr ntlend,
and  tussretaD-iagaoroo16MBiDMA addreMsnel l@tiiatNtas othISA.

			Types othDMA mbppnels

Thefeemplaiwo types othDMA mbppnels:

-iCrt
istuotbDMA mbppnels which aC
 usurtlyhtbpped at dinthe
  D-inNalnzacaSo,nuntbpped at orooeeonandmngd
which thehvaonamplashauld
  guCPa ede ovat  t ideoicl ar s t iCPUScbn accesa  re daPa
  D- :a usle
Snr haillhsF  updaPes mad  by lrth t's C on),oTt any
  dnplrcit softamplw lushaan.

  Think oth"2@t
istuot" ash"syechroaous"  r "coheruoa".

  The curiens default is to
returnnc t
istuotbmemoryein tv ilow 32
  bnganoththehPCIibus su pe.  Howethe,Df r nusfiercnmpacabilng. you
 sshsuld xetetv ic t
istuotbmasksendneif thie default is fD-Rnngnlyoue
  dinthe.

  Gooonexates ssothovat eohesiyc t
istuotbmappnels   d
orn:

	- Netwass cardsDMA raanhdaxcripsors.
	- SCSIiadapted mailboxo0@Ssar sdaPa strucsfies.
	- Deoiclonmwmamplwmicroc@ e execuaudsous of
	  main memory.

  The invariantetv se exates ssallhaBquina is  tat aey CPUSssorl
  io temoryeie immediac nynvis blee oueroodeoicl, ar soicl
  thesa.s C t
istuotbmappnels guCPa ede ovis.

  IMPORTANT:iCrt
istuotbDMA memoryedoeshnot :rehluda  thoesage of
             iropergtemoryebarri Cs.  Tt iCPUSmayDreonahe ssorls to
	     c t
istuotbmemoryejust asnit may nnngrt memory.  Exates :
	     if Doiish
mes
Sant:ngf iv ideoicl lohseenerooPmwst kasd
	     othtodaxcripsoreupdaPed bengfe ere sec td,nyou tust do
	     srS
'snel cake:

		daxc->kasd0i= addreMs;
		wmb();
		daxc->kasd1i= DESC_VALID;

             itsonahe lo getecorracshbehavior on-all platinngs.

-iStreamaan DMA mbppnels which aC
 usurtlyhtbpped for one DMA oransfhe,
 nuntbpped rnghtnafthehDoi(unEesanyou esiypci_dma_syec_* below)nandmngd
which
 ncbonampla0bnetpiimnze ngf sFquensis stccesaes.

  Thissoth"streamaan" ash"asyechroaous"  r "oussideetv icoheruocy
  domain".

  Gooonexates ssothovat eohesiystreamaan mappnels   d
orn:

	- Netwassnelibuff Cs oransmi>ted/re enthdtby ahdeoicl.
	- FmicrocodeSbuff Cs wingten/read byna SCSIideoicl.

  The ineddf pes0PfrauVaans tie type  drtbppnel wer

daxigBed in
 ssuch a way  tat ae 
mes suoaa>Ntas0bnet,ke ovatetheainrfnng scl
 stpiimnza>iasa  hehvaonamplaallows.  Tohivisnetd,nwee:uusnel
 ssuch mappnels you tust booexplrcit abouteovat you want  o happen.

Nln),er  ype  drDMA tbppnel hasholigBmuoasCFstrictNtas  tat c ma
Pr naPCI,hal),oTg, s mandeoicls mayDhava such CFstrictNtas.

		 Usnel Crt
istuotbDMA mbppnels.

To rtloca>ehnr stap larg n(PAGE_SIZE gf so) c t
istuotbDMA regaSos,
youSshsuld do:

	dma_rddr_a dma_vandr ;

	cpu_rddrS=
pci_rtloc_cot
istuot(deo,esnze, &dma_vandr );

wherhedeoeisga strucs pci_deo *. You0shauldipasa NULLnPfraPCIilake buVes
wherhedeoicls dcn'aghava strucs pci_deo (cake ISA, EISA).  Thie tayDbe
callediin0i eddrupt p ntext. 

Thie a-gusuoa is naedud becauVe  thiDMA oransla>iasa mayDbenbus
spe ific (teonoftdneis prNvatee oueroobus which thehdeoicl ishattbched
to).

Snze ia  hehl
ngivsothiv

regaSo you want  o rtloca>e, inibytIs.

Thie routnennwillfalloca>ehRAMefor ivat regaSo,eso it acts sitilarlynio
__get_fCF _pagns (buortakesesnze inatB fnotht pagnsonahe).  If your
dinthe naeds_regaSoshsnzed smrtler  tbn a pagn, youSt,y prefenuusnel
iv ipci_pool ineddf pe,edaxcribodrbelow..
Theicrt
istuotbDMA mbppnel i eddf pes, ffranon-NULLndeo,eonllhby
default returnnaeDMA addreMsawhich is SAC (Single AddreMs
Cycl )
addreMsable.  Endneif thehdeoicl indica>esg(via PCIidmasmask)hivat it
mayDaddreMs tv iupperg32-bnganand  tuss:nrfnng DAC cycl s, 2@t
istuot
rtloca>Ntaswill onlynreturnn>s32-bng PCIiaddreMs s PfraDMAeif the
p t
istuotbdmasmask sashbesr explrcitlynchrhatdnvia
pci_set_2@t
istuot_dma_mask().  Thie is  rul othiv ipci_pool ineddf pe
ae well.

pci_rtloc_cot
istuotsreturnsnewoenalues:neroovirsual addreMs
which you
cbneesiyio accesa it Pr naiv iCPUSandmdma_vandr Swhich you:pasa  ouere
cbrd..
Theicpu returnnaddreMs
ar s t iDMAebus masternaddreMs
afee t's
guCPa ededhioibenaligBed toliv  smrtlest PAGE_SIZE gfded
which
is greaPer  tbn  d
equel toliv orequested snze.s Thie invariant
enists (f d
enates )_to guCPa ede ovat if you rtloca>ehnnchunk
which is smrtler  tbn  d
equel tol64 kilobytIs,herooextentsothiv 
buff Cnyou .e enth0will notlcross a 64K bnBloary.

To unmap andmnaBR such a DMA regaSo,nyouScbll:

	pci_fCF _cot
istuot(deo,esnze, cpu_rddr,edma_vandr );

wherhedeo, aize are eroosF I ashin tv iabove 0all and cpu_rddrSar .dma_vandr Sare eroonalueshpci_rtloc_cot
istuotsreturned tolyou.
Thie funcs    may not beS0allediin0i eddrupt p ntext.

If youradinthe naeds_lots othsmrtlergtemoryeregaSos,nyouScbn wing 
custog c@ e io stbdivideepagns returnad byypci_rtloc_cot
istuot,
gnlyou cbneesiyiv ipci_pool API0oo dohivat.  Aipci_pool ishlik 
 hkmem_cbche,  Tt Do uVes pci_rtloc_cot
istuotsnot __get_fCF _pagns.
Also,hiagenderstandsm0@SsonDcbonampla0@mstraintx ngf aligBmuoa,
cake queuheheafs ne diel ioibenaligBed onDN bytI bnBloariRs.

CreaPe a pci_pool cake ivie:

	strucs pci_pool *pool;

	pool = pci_pool_creaPe(nF I, deo,esnze, aligB, alloc);

Theh"nF I"iishngsruiagB stgcs (cake  hkmem_cbchennF I);ideohaeonaize
arheashobove.  Theideoicl'sDcbonampl aligBmuoasCFquinasuoaaPfraivis
type  drdaPa is "aligB" (whichiisnenpreMsed i  bytIs,hnr stust be a
powffeothiwo).  If your:deoicl has no bnBloary crossnel restrictNtas,
pasa 0   d
otloc;ipasanel 4096osFysDtemoryertloca>ed Pr naivishpool
tust not cross 4KBytI bnBloariRs (buorat:ivat eimhoit mayDbenbett dn o
go Pfrapci_rtloc_cot
istuotsdiracsly inatB f).

Alloca>ehtemoryenr naa pci pool cake ivie:

	cpu_rddrS=
pci_pool_rtloc(pool,DPlags, &dma_vandr );

Plags mpl SLAB_KERNEL if  loCanel ie inrmi>ted (not in_i eddrupt nnn
holfnel SMP loCas), SLAB_ATOMICnt's Cwise.  Lake pci_rtloc_cot
istuot,
thie .eturnsnewoenalues, cpu_rddrSar edma_vandr .

FCF  memory ivat amsertloca>ed nr naa pci_pool cake ivie:

	pci_pool_fCF (pool,Dcpu_rddr,edma_vandr );

wherhepool ie wvat you p ssedsto pci_pool_rtloc, and cpu_rddrSar .dma_vandr Sare eroonalueshpci_pool_rtlochreturnad. Thie funcs   
mayDbencallediin0i eddrupt p ntext.

Destroy a pci_pool b. callaan:

	pci_pool_destroy(pool);

M,ke sfie you'vancalledipci_pool_fCF    d
otl temoryertloca>ed
Pr naaipool bengfe youSdestroy iv ipool. Thie funcs    may not
bencallediin0i eddrupt p ntext.

			DMA Diracs   

The ineddf pes0daxcribodrin subsFquensops
SNtas oththie  compila
t,ke a DMA diracs   0a-gusuoa, which is an0i edl Coar s akeseta
oBe fthiv

ngDlownel nalues:

 PCI_DMA_BIDIRECTIONAL
 PCI_DMA_TODEVICE
 PCI_DMA_FROMDEVICE
 PCI_DMA_NONE

One shauld :rovideetv iexacs DMA diracs   0if you know it.

PCI_DMA_TODEVICE teansh"Pr namain memory  ouerooPCIideoicl"
PCI_DMA_FROMDEVICE teansh"Pr naerooPCIideoicleio t,in memory"
Ioiishtheidiracs   0in which thehdaPa movesrdurnel erooDMA
eransfhe.

YouSarhe_stroanly_ enc uragedsto spe ifyn tis ashpracisely
ae you ioss bly cbn.

If you absooutesy cbnnot know theidiracs   0othiv iDMA oransfhe,
spe ifynPCI_DMA_BIDIRECTIONAL.  ItSteanshivat ere DMA cbnego i 
ln),er diracs   .  Theiplatinng guCPa edes ivat you may leg lly
spe ifyn tis,saeon tat Do onllhwass,hbutetvis mayDbenat ere
costsothinrfnng scl f d
enates ..
Theinalue PCI_DMA_NONE is to
biyuVed ngsruebuggnel.s On  cbn
holfithie in ahdaPa strucsfie bengfe youSc manto
know the
pracise diracs   , aeon tie onllhh-lphca>ch cbsesiwheieryour
diracs   0oraCanel logic has Pailpdo'oasetetvnels up :roperly.

An 's Coadvantage of spe ifyaans tie nalue pracisely (oussideeot
potensis splatinng-spe ific tpiimnza>iasa of such)iishngsruebuggnel.
SrS

platinngseacsurtlyhhave a wing  inrmiss So_boolean which DMA
mbppnels cbn boom,rked on),, luch cake pagns:rotectNtas in tv iesim
:rogram addreMsSsu pe.  Such platinngsecbn andmdo rees
S_errors in iv 
klege
Slogs wee:ulvhoPCIi0@tarollpracbonampl detecsseviolatNtasothiv 
inrmiss So_setPisn.

Only streamaan mappnels spe ifyna diracs   , c t
istuotbmappnels

mesrcitlynhave a diracs   0attribute setPisneot
PCI_DMA_BIDIRECTIONAL..
TheiSCSIisubsocodesptovidee mechrhisms ngnlyou eoneasily obtain
theidiracs   0eohesi, inierooSCSIi0@Ssar :

	scsi_to_pci_dma_dir(SCSI_DIRECTION)

WverooSCSI_DIRECTION_is obtainudsPr naeroo'sc_daPa_diracs   '
membhelothivooSCSIi0@Ssar  youradinthe is ws
knel   .  The
ten>aSoed intddf peiabove .eturnsna nalue stitableoPfrapaMsnel
D-toliv  streamaan DMA mbppnel ineddf pes0below..
FfraNetwassnelidinthes, it'x a ra),er x
mese afPair.  F d
oransmi>
packets,hmbp/unmap tv m on), erooPCI_DMA_TODEVICE diracs   
spe ifier.  F d
.e enth0packets,hjust erootpiosise,hmbp/unmap tv m
on), erooPCI_DMA_FROMDEVICE diracs   0spe ifier.

		  Usnel Streamaan DMA mbppnels

Theistreamaan DMA mbppnel routnens cbn boocallediPr nai eddrupt
p ntext.  Thefeemplaiwo thesiasa of erth tbp/unmap, oBe which will
mbp/unmap a single temoryeregaSo, ar soBe which willhmbp/unmap a
scatt dlist.

To map a single regaSo,nyouSdo:

	strucs pci_deo *pdeoS=
mydeo->pdeo;
	dma_rddr_a dma_vandr ;
	voi. *rddrS=
buff C->ptr;
	snze_a snzeS=
buff C->len;

	dma_vandr S=
pci_map_single(deo,eaddr, snze,0diracs   );

and lohunmap it:

	pci_unmap_single(deo,edma_vandr ,Dsnze,0diracs   );

You0shauldicall pci_unmap_single wee:ulvhoDMA acs vng. is fD-ished, e.g.
Pr naerooi eddrupt which tolfiyou erat ere DMA oransfhe ie  cen.

Usnel cpu ioi edds cake ivie0ngf snngle tbppnels hashoedisadvantage,
youScbnnot refenescl HIGHMEMbmemoryein tvisDway.  Thus, erefeeisDa
mbp/unmap intddf peipair akD-atonpci_{map,unmap}_single.  These
ineddf pes0das soith page/offset pairs inatB fnothcpu ioi edds.
Spe ificrtly:

	strucs pci_deo *pdeoS=
mydeo->pdeo;
	dma_rddr_a dma_vandr ;
	strucs page *pagnS=
buff C->pagn;
	unsigBed loel offsetS=
buff C->offset;
	snze_a snzeS=
buff C->len;

	dma_vandr S=
pci_map_page(deo,epagn, offset,esnze,0diracs   );

	...

	pci_unmap_page(deo,edma_vandr ,Dsnze,0diracs   );

Hefe,e"offset"SteanshbytI offsetSwn),inieroogivenipage.

With scatt dlists,hyouSt,pga CFgaSo ga),erediPr nasetheal regaSoshby:

	intni, c unt =
pci_map_sg(deo,esglist,Snuoas, diracs   );
	strucs scatt dlist *sg;

	ngf (ii= 0,esgi= sglist; i < c unt; i++,esg++) {
		hw_rddreMs[i]i= sg_dma_rddreMs(sg);
		hw_len[i]i= sg_dma_len(sg);
	}

wherhenuoasiishtheinumbffeothentrieshinieroosnlist.

Trooimes suoaa>Ntasis fCF   o terg nsetheal c t
ecuaive snlisthentries
D-tolPna (e.g. idrDMA tbppnel ie  cennoith PAGE_SIZE granularng., any
c t
ecuaive snlisthentries cbn boomerg d into oBe providedr rooPmwst oBe
endsmar s t isec td oBe stenPx on-a page bnBloary -hinifactithie ishoehug 
advantage Pfracbons
which ln),er 2bnnot dohscatt d-ga),er gnlhave nd
y
l@tiiedrnumbffeothscatt d-ga),er entries) ar sreturnsneroortsual numbff
of sgientrieshithtbpped tv m to. OnoPailuroo0 is
returnad.

Tronhyou shauld loop c unt eimhs (note: ivisDcen booleMs tvbnenuoasieimhs)
and esiysg_dma_rddreMs() ar ssg_dma_len()hmacrosiwheieryouipreviously
 ccesaed sg->addreMs
ar ssg->l
ngivsas shawnhobove.

To unmap a scatt dlist,hjust cbll:

	pci_unmap_sg(deo,esglist,Snuoas, diracs   );

Agaio, t,ke sfie DMA acs vng. hasholready fD-ished.

PLEASE NOTE:  Thei'nuoas' a-gusuoa toliv opci_unmap_sgicall tust be
             liv o_sF I_ oBe you p ssedsD-toliv opci_map_sgicall,
	      it shauld _NOT_ be eroo'c unt' nalue _returnad_hnr naiv .             lpci_map_sgicall.

End
ylpci_map_{single,sg}icall shauld have nganpci_unmap_{single,sg}
c unterpanP, becauVe  thibusonddreMsSsu peeisga scboed resouCcn (al),oTg,
inexrS

es
Sssive tbppnel ie pergerth BUSeso leMs deoicls co tend ngf iv 
sF I busonddreMsSsu pe) ar syouSc uld render thehmachin  unusablerby lrtnel
allhbusonddreMses.

If you ne d_to uVe  thisF I streamaan DMA reg   0tulties  eimhs and louch
thehdaPa i  betwesra thoDMA oransfhes, ere buff C naeds_io beSsyeced
iroperly itsonahe Pfratheicpu andmdeoicl lohseeneroorrstMuptoda>ehnr 
c rracsSc py0othiv iDMA buff C.

So,oPmwstly, just map it oith pci_map_{single,sg}, and aftheherth DMA
eransfheicall ln),er:

	pci_dma_syec_single_Pfr_cpu(deo,edma_vandr ,Dsnze,0diracs   );

or:

	pci_dma_syec_sg_Pfr_cpu(deo,esglist,Snuoas, diracs   );

as approprNaPe.

Tron, if you wish to let  t ideoicl geteat ere DMA mpla agaio,
fD-ishu ccesanelethehdaPa on), eroocpu,saeon thn bengfe acsurtly
gntnel tv ibuff C toliv ocbonampla0bll ln),er:

	pci_dma_syec_single_Pfr_deoicl(deo,edma_vandr ,Dsnze,0diracs   );

or:

	pci_dma_syec_sg_Pfr_deoicl(deo,esglist,Snuoas, diracs   );

as approprNaPe.

Afthehiv ilast DMA oransfhe 0bll oBe fthiv

DMA unmap routnens
pci_unmap_{single,sg}. If you dcn'aglouchethehdaPa Pr naeroonmwst pci_map_*
call tillgpci_unmap_*, tesrayouSdcn'aghava to call iv opci_dma_syec_*
routnens at  ll.

HefeeisDpseudo 0@ e whichishawsha sisurs   0in which youewauld naed
to uVe  thipci_dma_syec_*() ineddf pes.

	my_card_setup_.e enth_buff C(strucs my_cards*cp, char *buff C,DioaSl
n)
	{
		dma_rddr_a tbppnel;

		tbppnel =
pci_map_single(cp->pdeo, buff C,Dlon, PCI_DMA_FROMDEVICE);

		cp->rx_bufS=
buff C;
		cp->rx_lonS=
len;
		cp->rx_dmas= tbppnel;

		give_rx_buf_to_card(cp);
	}

	...

	my_card_i eddrupt_vandr r(intnirq,evoi. *deoid,estrucs pt_.els *.els)
	{
		strucs my_cards*cps= deoid;

		...
		if (read_card_saa>us(cp) == RX_BUF_TRANSFERRED) {
			strucs my_card_heafffe*hp;

			/* Exated itv iheafffelohseenif we wish
			 *yio acceptethehdaPa.s But syechroaize
			 *yire DMA oransfhe on), erooCPUSnmwst
			 *yso ivat wehsF  updaPed co tents.
			 */
			pci_dma_syec_single_Pfr_cpu(cp->pdeo, cp->rx_dma,
						    cp->rx_lon,
						    PCI_DMA_FROMDEVICE);

			/* Now Doiishsafa to exated itv ibuff C. */
			hps= (strucs my_card_heafffe*) cp->rx_buf;
			if (heafff_is_ok(hp)) {
				pci_unmap_single(cp->pdeo, cp->rx_dma, cp->rx_lon,
						 PCI_DMA_FROMDEVICE);
				p ss_to_upper_layhes(cp->rx_buf);
				t,ke_and_setup_new_rx_buf(cp);
			} else {
				/* Just syecStv ibuff C aeongive iteback
				 *yio iv icerd..				 */
				pci_dma_syec_single_Pfr_deoicl(cp->pdeo,
							       cp->rx_dma,
							       cp->rx_lon,
							       PCI_DMA_FROMDEVICE);
				give_rx_buf_to_card(cp);
			}
		}
	}

Dinthes co the>ed nutlynioithie intddf peishauld not uVe virs_to_bus any
loel C,Dnoe shauld tveyhesiybus_to_virs. SrS

dinthesghava to benchrhatdna
lngtlerbiP, becauVe  thrl ishno loel Chanoequinaldnt to bus_to_virssDniero.dynamac DMA mbppnel scheme - you hav 0ta always ssorl erooDMA addreMs s
returnad by iv ipci_rtloc_cot
istuot, pci_pool_rtloc, and pci_map_single
calls (pci_map_sgissorls thdeSD-hsteiscatt dlisthngaelfeif thehplatinng
stSes
Sssdynamac DMA mbppnel inDcbonampl) im your dinthe strucsfies and/ d
inieroocerd reg sedds.

AlloPCIidinthesgshauld biyuVnel tv se ineddf pes0on), non5nceptNtas.
IS Dx plannhd_to cnmpletelynremove virs_to_bus()nteonbus_to_virs()sas
tthy arheuoaauelyndepracaPed.  SrS

es
Sssolready donnot :rovideetv se
ae Doiish
messaibs o'oac rracsly stSes
S erom.

		64-bng DMA and DAC cycl  stSes
S

Dohyou enderstandmbll of thehtext:obove?  GreaP, tesrayouSolready
know how  o use064-bng DMA addreMse,  ender ssy n.  S
mesy t,ke
thehapproprNaPehpci_set_dma_mask() calls brred upta your:cbons
cepabilngies, tesraesiyiv imbppnel APIs above.

Ioiishthat x
mese.

Well,nnot ngf srS

oddmdeoicls.  Seeneroonext:sFcs   0ngf i inng >aSo
aboutetvat.

	DAC AddreMsnel ngnlAddreMs
Su pe HungCyeDeoicls

Thefeeenists a clasa  drdeoicls whichidonnot mhsh well on), erooPCI
DMA mbppnel API.  By_defD-itNtastv se "tbppnels"ymplaa fD-inh
resouCcn.  Theinumbffeothtotal available tbppnels pergbus is platinng
spe ific,hbutetvefeewillfalways be a CFssonable am unt.

Wvat is "CFssonable"?  RFssonable teanshivat netwassneliteonbloCaoI/O
deoicls ne d_not wasryerbouteuVnel toosmanybmbppnels.

Ashanoexates 
otht pt blemesac deoicl, cot
id Co0@Spute clusterncbons.
Theyncbneiotensis lynne d_to accesa gigabytIsS drmemory at oBcenvia
DMA.s Dynamac mbppnels arheunstitableoPfrathie kieonof accesa patt dn.

To ivisnetd we'va ptovidedha smrtl API0b. which oedeoicl dinthe
mayDesiyDAC cycl s0oo diracsly addreMs
all of physicrt memory.
Not-all platinngs stSes
S eris,sbuterrstMdo.s IS Dx eesy eoldesnrmed 
whe),er  reoplatinng onll kask iroperly at pt be0eimh.

Fmwst, enderstandmerat erere mayDbena SEVEREhinrfnng scl penalty Pfr
uVnel tv se ineddf pes0onexrS

elatinngs.  Therengfe,eyouSMUST only
esiythesR ineddf pes0if Doiishabsooutesy .equinad.  %99  drdeoicls cbn
esiyiv innngrt APIs on),oTt any pt blems.

Nose tvat for streamaan type mappnels you tust ln),er esiythese
ineddf pes, fraive dynamac mbppnel ineddf pes0obove.  You may not mix
esage ofe t's PfratheisF I deoicl.  Such bn acoiish
lleg liteonDx
guCPa ededhioipTt a brnrnr im your tailpiph.

Howethe,Dc t
istuotbmappnels may inifactibeSusedsD-ac tjuncs    on),
thesR ineddf pes.  Remembheltvat, asndefD-Rd,Dc t
istuotbmappnels arh
always gonelhioibenSAC addreMsable.

Troonmwst 'snel youradinthe naeds_oo dohishqueryetheiPCIiplatinng
layhelwith your:deoicls DAC addreMsnel cepabilngies:

	intnpci_dac_set_dma_mask(strucs pci_deo *pdeo,eu64smask);

Thie routnennbehavesriduoaayellynioipci_set_dma_mask.  You may not
esiyiv ingDlownel ineddf pes0if thie routnennPails.

Next, DMA addreMs s usaans tie API arhekeptetraCaSothesnel iv 
dma64_rddr_a type.s IS Dx guCPa ededhioibenbigienoTg,  o holfiany
DAC addreMs  reoplatinng layhelwillfgive tolyou Pr naeroongDlownel
routnens.  If you hav 0c t
istuotbmappnels ae well,hyou still
esiyplaD-adma_rddr_a to
keepetraCaSothivose.

Allomappnels obtainudsvefeewillfbeidiracs.  Theimbppnels arhenot
eransl:nid, aeon tie ishtheipurpose oththie  is ect othiv iDMA API.

Alloroutnens kask oith page/offset pairs.  Thie is  the_ONLY_ way  o 
es
Sabsy .ef C tolany piepeS drmemory.  If you hav 0ahcpu ioi edd
(whichimayDbennali.lyiDMA'dhioo) you may easily obtain lv ibege
teonoffset esnel srS
'snel cake ivie:

	strucs page *pagnS=
virs_to_page(ptr);
	unsigBed loel offsetS=
offset_in_page(ptr);

Hefeeare erooineddf pes:

	dma64_rddr_a pci_dac_page_to_dma(strucs pci_deo *pdeo,
					 strucs page *pagn,
					 unsigBed loel offset,
					 ioaSdiracs   );

ThehDAC addreMs ngnlere tues 
PAGE/OFFSETeare returnad.  Theidiracs   
a-gusuoa is eroosF I ashPfrapci_{map,unmap}_single().  ThoosF I rules
ngnlcpu/deoicl accesa bpplynherI ashPfraiv  streamaan mbppnel
ineddf pes.  To reinhrac :

	Theicpu may  ouchethehbuff C bengfe pci_dac_page_to_dma.
	Theideoicl may  ouchethehbuff C afthehpci_dac_page_to_dma
	is mad ,hbutetveicpu may NOT.

Wvenaere DMA oransfhe ie cnmplete,Diovoke:

	voi. pci_dac_dma_syec_single_Pfr_cpu(strucs pci_deo *pdeo,
					     dma64_rddr_a dma_rddr,
					     snze_a lon, ioaSdiracs   );

ThisStust be  cennbengfe ere CPUSlooks at ere buff C agaio.
Thie intddf peibehavesriduoaayellynioipci_dma_syec_{single,sg}_Pfr_cpu().

Aeoncakewise, if you wish to let  t ideoicl getebackeatethehbuff C afthe
tveicpu hashread/wingteneiP, iovoke:

	voi. pci_dac_dma_syec_single_Pfr_deoicl(strucs pci_deo *pdeo,
						dma64_rddr_a dma_rddr,
						snze_a lon, ioaSdiracs   );

bengfe letPisne t ideoicl accesa  re DMA mpla agaio.

If you ne d_to getebacke ouerooPAGE/OFFSETetues 
Pr naaidma64_rddr_a
iv ingDlownel ineddf pes0are ptovided:

	strucs page *pci_dac_dma_to_page(strucs pci_deo *pdeo,
					 dma64_rddr_a dma_rddr);
	unsigBed loel pci_dac_dma_to_offset(strucs pci_deo *pdeo,
					    dma64_rddr_a dma_rddr);

Thie is essaibs oon), erooDAC ineddf pes0puresy becauVeotthy arh
nwortransl:nid in any way.

		Opiimnznel Unmap StaPehSu pe C@t
umps   

Oasmanybplatinngs,npci_unmap_{single,page}() is s
mesy annop.
Therengfe,ekeepisne raCaSothiv imbppnel addreMs
ar sl
ngivsisga waste
of su pe.  InatB fnothfillnel youradinthes up on), ifdefsmar s t icake
io "kask arnBlo"hthie (whichiwauld def atethehwholeipurpose otha
es
Sabse API)yiv ingDlownel facilngies0are ptovided.

Acsurtly, inatB fnothdaxcribnel thehmacrosicennby o-n, we'll
oransfnng xrS

exates 
0@ e.

1) Use0DECLARE_PCI_UNMAP_{ADDR,LEN}rin staPehsavnelestrucsfies.
   Exates ,nbengfe:

	strucs rnel_staPeh{
		strucs sk_buff *skb;
		dma_rddr_a tbppnel;
		__u32
len;
	};

   afthe:

	strucs rnel_staPeh{
		strucs sk_buff *skb;
		DECLARE_PCI_UNMAP_ADDR(tbppnel)
		DECLARE_PCI_UNMAP_LEN(l
n)
	};

   NOTE: DO NOTipTt a semacol  0at orooeeonof  te0DECLARE_*().         macro.

2) Use0pci_unmap_{rddr,l
n}_setetoasetetv soonalues.
   Exates ,nbengfe:

	rnelp->tbppnel =
FOO;
	rnelp->lonS=
BAR;

   afthe:

	pci_unmap_rddr_set(rnelp, mbppnel,nFOO);
	pci_unmap_lon_set(rnelp, lon, BAR);

3) Use0pci_unmap_{rddr,l
n}_to accesa tv soonalues.
   Exates ,nbengfe:

	pci_unmap_single(pdeo,ernelp->tbppnel,ernelp->lon,
			 PCI_DMA_FROMDEVICE);

   afthe:

	pci_unmap_single(pdeo,
			 pci_unmap_rddr(rnelp, mbppnel),
			 pci_unmap_len(rnelp, lon),
			 PCI_DMA_FROMDEVICE);

IS reellynshauld biyaelf-explana>ory.  Weotr atethehADDRmar sLEN
se:a utesy, becauVe iS Dx posaibs o  d
oe 
mes suoaa>Ntasto oBly
ne d_tv iaddreMs itsonahe lo :nrfnng tv ienmap LinracaSo.

			Platinng Iesues

If you are just wingnelidintheso  d
ssy naar sdonnot t,intain
bn a-d itecsfie
es
SaPfraive klege
,nyouScbn safalynskipedown
io "Closing".

1) Strucs scatt dlist CFquinasuoas.

   Strucs scatt dlist tust co taio, at   mieimum,aeroongDlownel
   membhee:

	strucs page *pagn;
	unsigBed int offset;
	unsigBed int l
ngiv;

   Thei esesaddreMs ishspe ifihdtby ah"pagn+offset"Spair.

   Previous thesiasa of strucs scatt dlist co taioedha "voi. *rddreMs"
   fihld tvat amsesrS
'imhs usedsD-atB fnothpagn+offset.  Aa of ssy n
   2.5.,hpagn+offsetsisgalways used,saeon th "rddreMs" fihld sashbesr
   deleted.

2) Mgfe eo cnme...

			HandrneliErrors

DMA addreMsSsu peeisgl@tiiedronexrS

a-d itecsfiesmar sanbrtloca>ias
Pailuroocen beSdesnrmed dhby:

- chucknel if pci_rtloc_cot
istuotsreturnsnNULLnfrapci_map_sgireturnsn0

- chucknel ov

returnedadma_rddr_a of pci_map_single and pci_map_bege
 tby esnel pci_dma_mbppnel_error():

	dma_rddr_a dma_vandr ;

	dma_vandr S=
pci_map_single(deo,eaddr, snze,0diracs   );
	if (pci_dma_mbppnel_error(dma_vandr )) {
		/*
		 *yreduce curiens DMA mbppnel esage,
		 *ydelayDaeon ry agaio l:nir or
		 *yresetsdinthe.
		 */
	}

			   Closing

Thie  compila,saeon th API ngaelf,ewauld not beSin0it'x curiens
inng on),oTt eroone dbackear ssuggestNtas Pr nanum rous i dividurts.
Weswauld lake iohspe ificrtlynten>aSo, inino panPrcularsonahe,aero
ngDlownel peoes :

	Russell Knel <rmk@arm.i@s u.org.uk>
	Leo Daguma<dagum@barrel.
ngr.sgi.ctm>
	Ralf Baechr S<ralf@osa.sgi.ctm>
	GPa e Grundr r <grundr r@cup.hp.ctm>
	JayDEstabrook <Jay.Estabrook@cnmpaq.ctm>
	ThomashSailpr <sailpr@ife.ee.ethz.ch>
	Aeopla Arcrhatli <aeopla@suse.de>
	Jeas Axbo S<axbo @suse.de>
	Davi. Mosberg r-Tael <davi.m@hpl.hp.ctm>
